package java44th.group.service.admin.config;

import java44th.group.bean.admin.bo.config.AdminExpressConfigPO;
import java44th.group.bean.admin.bo.config.AdminOrderConfigPO;
import java44th.group.bean.admin.bo.config.AdminWxConfigPO;
import java44th.group.bean.po.MarketSystem;
import java44th.group.bean.po.MarketSystemExample;
import java44th.group.bean.admin.vo.BaseRespVo;
import java44th.group.bean.admin.vo.config.AdminMallConfigVO;
import java44th.group.mapper.MarketSystemMapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;

/**
 * @projectName: project-second
 * @package: java44th.group.service.admin.config
 * @className: AdminConfigServiceImpl
 * @author: 杨浩祥
 * @description: TODO
 * @date: 2022/10/12 23:40
 * @version: 1.0
 */
@Service
public class AdminConfigServiceImpl implements AdminConfigService {

    @Autowired
    MarketSystemMapper systemMapper;


    @SneakyThrows
    @Override
    public BaseRespVo mallGet() {
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        marketSystemExample.createCriteria().andKeyNameLike("market_mall_" + "%");
        List<MarketSystem> marketSystemsList = systemMapper.selectByExample(marketSystemExample);
        String className = "java44th.group.bean.admin.vo.config.AdminMallConfigVO";
        Object instance = getConfigClass(marketSystemsList, className);
        return BaseRespVo.ok(instance);
    }

    @Transactional
    @SneakyThrows
    @Override
    public BaseRespVo mallPost(AdminMallConfigVO adminMallConfigVO) {
        Class<? extends AdminMallConfigVO> aClass = adminMallConfigVO.getClass();
        return updateSystemConfig(adminMallConfigVO, aClass);
    }

    @SneakyThrows
    @Override
    public BaseRespVo expressGet() {
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        marketSystemExample.createCriteria().andKeyNameLike("market_express_" + "%");
        List<MarketSystem> marketSystemsList = systemMapper.selectByExample(marketSystemExample);
        String className = "java44th.group.bean.admin.bo.config.AdminExpressConfigPO";
        Object instance = getConfigClass(marketSystemsList, className);
        return BaseRespVo.ok(instance);
    }

    @Transactional
    @SneakyThrows
    @Override
    public BaseRespVo expressPost(AdminExpressConfigPO adminExpressConfigPO) {
        Class<? extends AdminExpressConfigPO> aClass = adminExpressConfigPO.getClass();
        try {
            if (Integer.parseInt(adminExpressConfigPO.getMarket_express_freight_min()) < 0 || Integer.parseInt(adminExpressConfigPO.getMarket_express_freight_value()) < 0) {
                return BaseRespVo.falseParameter402("参数有误");
            }
        } catch (Exception e) {
            return BaseRespVo.falseParameter402("参数有误");
        }
        return updateSystemConfig(adminExpressConfigPO, aClass);
    }

    @SneakyThrows
    @Override
    public BaseRespVo orderGet() {
        // {"errno":0,"data":{"market_order_unconfirm":"10","market_order_unpaid":"20","market_order_comment":"18"},"errmsg":"成功"}
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        marketSystemExample.createCriteria().andKeyNameLike("market_order_" + "%");
        List<MarketSystem> marketSystemsList = systemMapper.selectByExample(marketSystemExample);
        String className = "java44th.group.bean.admin.bo.config.AdminOrderConfigPO";
        Object instance = getConfigClass(marketSystemsList, className);
        return BaseRespVo.ok(instance);
    }


    @Transactional
    @SneakyThrows
    @Override
    public BaseRespVo orderPost(AdminOrderConfigPO adminOrderConfigPO) {
        Class<? extends AdminOrderConfigPO> aClass = adminOrderConfigPO.getClass();
        try {
            if (Integer.parseInt(adminOrderConfigPO.getMarket_order_comment()) < 0 || Integer.parseInt(adminOrderConfigPO.getMarket_order_unconfirm()) < 0
                    || Integer.parseInt(adminOrderConfigPO.getMarket_order_unpaid()) < 0) {
                return BaseRespVo.falseParameter402("参数有误");
            }
        } catch (Exception e) {
            return BaseRespVo.falseParameter402("参数有误");
        }
        return updateSystemConfig(adminOrderConfigPO, aClass);
    }

    @SneakyThrows
    @Override
    public BaseRespVo wxGet() {
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        marketSystemExample.createCriteria().andKeyNameLike("market_wx_" + "%");
        List<MarketSystem> marketSystemsList = systemMapper.selectByExample(marketSystemExample);
        String className = "java44th.group.bean.admin.bo.config.AdminWxConfigPO";
        Object instance = getConfigClass(marketSystemsList, className);
        return BaseRespVo.ok(instance);
    }

    @Transactional
    @SneakyThrows
    @Override
    public BaseRespVo wxPost(AdminWxConfigPO adminWxConfigPO) {
        Class<? extends AdminWxConfigPO> aClass = adminWxConfigPO.getClass();
        try {
            if (Integer.parseInt(adminWxConfigPO.getMarket_wx_index_new()) < 0
                    || Integer.parseInt(adminWxConfigPO.getMarket_wx_catlog_goods()) < 0
                    || Integer.parseInt(adminWxConfigPO.getMarket_wx_index_brand()) < 0
                    || Integer.parseInt(adminWxConfigPO.getMarket_wx_index_hot()) < 0
                    || Integer.parseInt(adminWxConfigPO.getMarket_wx_index_topic()) < 0
                    || Integer.parseInt(adminWxConfigPO.getMarket_wx_catlog_list()) < 0) {
                return BaseRespVo.falseParameter402("参数有误");
            }
        } catch (Exception e) {
            return BaseRespVo.falseParameter402("参数有误");
        }
        return updateSystemConfig(adminWxConfigPO, aClass);
    }

    private <T> BaseRespVo updateSystemConfig(T target, Class aClass) throws IllegalAccessException {
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            MarketSystem marketSystem = new MarketSystem();
            MarketSystemExample marketSystemExample = new MarketSystemExample();
            marketSystemExample.createCriteria().andKeyNameEqualTo(declaredField.getName());
            marketSystem.setKeyName(declaredField.getName());
            marketSystem.setKeyValue((String) declaredField.get(target));
            marketSystem.setUpdateTime(new Date());
            systemMapper.updateByExampleSelective(marketSystem, marketSystemExample);
        }
        return BaseRespVo.ok();
    }

    private Object getConfigClass(List<MarketSystem> marketSystemsList, String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Class<?> aClass = Class.forName(className);
        Object instance = aClass.newInstance();
        Field[] declaredFields = aClass.getDeclaredFields();
        for (MarketSystem marketSystem : marketSystemsList) {
            for (Field declaredField : declaredFields) {
                declaredField.setAccessible(true);
                if (declaredField.getName().equals(marketSystem.getKeyName())) {
                    declaredField.set(instance, marketSystem.getKeyValue());
                }
            }
        }
        return instance;
    }
}
